package com.fingard.dsp.bank.directbank.mipay01;

import com.fingard.FGBiz;
import com.fingard.constant.Format;
import com.fingard.dsp.bank.directbank.bankInterface.IAcceptNotice;
import com.fingard.generalobj.NoticeReq;
import com.fingard.generalobj.NoticeResp;
import com.fingard.net.listener.SocketReceiver;

import java.io.File;
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.util.Date;
import java.util.Map;

public class MIPAY01AcceptNotice extends MIPAY01Base implements IAcceptNotice {

    @Override
    public NoticeResp dealNotice(NoticeReq pNtReq) throws Exception {

        NoticeResp resp = new NoticeResp();
        String p_reqs = pNtReq.getReqString();
        WriteBankLogLn("接收到请求：" + pNtReq.getFullString());

        String tmpFileName = FGBiz.DSPHOME_Parent + File.separator + "signnotify";
        try {
            if (p_reqs.length() == 0) {
                String tmpStrRet = "空报文";
                WriteBankLogLn("响应：" + tmpStrRet);
                resp.respBytes = tmpStrRet.getBytes(pNtReq.reqCharset);
                return resp;
            }


            File dir1 = new File(tmpFileName);
            if (!dir1.exists()) {
                dir1.mkdir();
            }

            Date a = new Date();
            File tmpFile1 = new File(tmpFileName + File.separator + Format.DateTime14Format.format(a) + ".txt");
            if (!tmpFile1.exists()) {
                tmpFile1.createNewFile();
                PrintStream ps = null;
                FileOutputStream fos = null;
                try {
                    fos = new FileOutputStream(tmpFile1);
                    ps = new PrintStream(fos);
                    ps.println(p_reqs);
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    if (fos != null) {
                        fos.close();
                    }
                    if (ps != null) {
                        ps.close();
                    }
                }
            }

            String p_req = "";
            if (p_reqs.contains("\n\n")) {
                //通过空白行截取字符串
                int index1 = p_reqs.indexOf("\n\n");
                p_req = p_reqs.substring(index1 + 2);
            } else {
                p_req = p_reqs;
            }

            WriteBankLogLn("支付结果通知报文：" + p_req);

            //写入文件的字符串
            StringBuilder reader_str = new StringBuilder();
            Map params = convertToMap(p_req);


            if (params != null) {
//				String respData = getVerifyData(params);
//				String signature = params.get("signature").toString();
                //验签公钥
//				String publicKey = getBankConfig().getValueAsString("publicKey");
//				boolean verify = isValidSignature(respData, signature, publicKey);
                boolean verify = true;

                if (verify) {
                    //签约、解约成功
                    reader_str.append("<ATSYH><TransResp><BankCode>MIPAY01</BankCode>");
                    reader_str.append("<TransType>1944</TransType>");
                    reader_str.append("<TransSeqID></TransSeqID>");
                    reader_str.append("<TransParam><DetailRecord><RespCode>" + params.get("isSuccess").toString() + "</RespCode>");
                    reader_str.append("<TransAmt>" + params.get("totalFee").toString() + "</TransAmt>");
                    String orderId = params.get("outOrderId").toString();
                    reader_str.append("<ReqSeqID>" + orderId + "</ReqSeqID>");
                    reader_str.append("<TransState>" + params.get("tradeStatus").toString() + "</TransState>");
                    reader_str.append("<PayInfoCode>" + params.get("tradeStatus").toString() + "</PayInfoCode>");
                    reader_str.append("</DetailRecord></TransParam></TransResp></ATSYH>");

                    File dir = new File(tmpFileName);
                    if (!dir.exists()) {
                        dir.mkdir();
                    }
                    File tmpFile = new File(tmpFileName + File.separator + orderId + ".txt");
                    if (!tmpFile.exists()) {
                        tmpFile.createNewFile();
                        PrintStream ps = null;
                        FileOutputStream fos = null;
                        try {
                            fos = new FileOutputStream(tmpFile);
                            ps = new PrintStream(fos);
                            ps.println(p_reqs);
                        } catch (Exception e) {
                            e.printStackTrace();
                        } finally {
                            if (fos != null) {
                                fos.close();
                            }
                            if (ps != null) {
                                ps.close();
                            }
                        }
                    }
                }
            } else {
                String tmpStrRet = "报文格式错误";
                WriteBankLogLn("响应：" + tmpStrRet);
                resp.respBytes = tmpStrRet.getBytes(pNtReq.reqCharset);
                return resp;
            }
        } catch (Exception e) {
            WriteBankLogLn(e);
        }
        String tmpStrRet = "";
        WriteBankLogLn("响应：" + tmpStrRet);
        resp.respBytes = tmpStrRet.getBytes(pNtReq.reqCharset);
        return resp;
    }

    @Override
    public void getContentLength(SocketReceiver p_receiver) throws Exception {
        // TODO Auto-generated method stub

    }

}
